package leetcode.medium.limit50;

/**
 * 最长回文子串
 *
 * @author liuyy
 * @date 2019/10/29 18:03
 **/
public class No5 {

    public static void main(String[] args) {

        No5 no5 = new No5();
        String s = no5.longestPalindrome("aba");
        System.out.println(s);

    }

    public String longestPalindrome(String s) {
        String res = "";
        for (int i = 0; i < s.length(); i++) {
            String palindrome = palindrome(s, i, i);
            String palindrome2 = palindrome(s, i, i + 1);
            res = res.length() > palindrome.length() ? res : palindrome;
            res = res.length() > palindrome2.length() ? res : palindrome2;
        }
        return res;
    }

    public String palindrome(String s, int l, int r) {
        while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            l--;
            r++;
        }
        return s.substring(l + 1, r);
    }
}
